/*
 * Filename: e:\OwnProject\same_app\lib\widgets\Loading copy.dart
 * Path: e:\OwnProject\same_app\lib\widgets
 * Created Date: Monday, February 22nd 2021, 3:47:37 pm
 * Author: chenfayun
 * 
 * Copyright (c) 2021 Your Company
 */

import 'package:flutter/material.dart';

class LoadingDialog extends StatefulWidget {
  final String loadingText;
  final Future<dynamic>? func;
  final Function? onFail;
  final Function? onSuccess;

  const LoadingDialog(
      {Key? key,
      this.loadingText = "loading...",
      this.func,
      this.onFail,
      this.onSuccess})
      : super(key: key);
  @override
  LoadingDialogState createState() => LoadingDialogState();
}

class LoadingDialogState extends State<LoadingDialog> {
  late String loadingText;
  @override
  void initState() {
    super.initState();
    loadingText = widget.loadingText;
    if (widget.func != null) {
      widget.func!.then((resp) {
        Navigator.pop(context);
        if (widget.onSuccess != null) widget.onSuccess!(resp);
      }).catchError((onError) {
        Navigator.pop(context);
        if (widget.onFail != null) widget.onFail!(onError);
      });
    }
  }

  resetText(String text) {
    setState(() {
      loadingText = text;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: Center(
        //保证控件居中效果
        child: SizedBox(
          width: 120.0,
          height: 120.0,
          child: Container(
            decoration: const ShapeDecoration(
              color: Color(0xffffffff),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(8.0),
                ),
              ),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                const CircularProgressIndicator(),
                Padding(
                  padding: const EdgeInsets.only(
                    top: 20.0,
                  ),
                  child: Text(
                    loadingText,
                    style: const TextStyle(fontSize: 12.0, color: Colors.black),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  closeDailog() {
    Navigator.of(context).pop();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}
